package com.yix.util;

import org.apache.poi.xwpf.usermodel.XWPFDocument;
import org.apache.poi.xwpf.usermodel.XWPFTableCell;
import org.apache.poi.xwpf.usermodel.XWPFTableRow;
import org.openxmlformats.schemas.wordprocessingml.x2006.main.STMerge;

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.HashMap;
import java.util.List;

/**
 * 根据doc模板生成
 */
public class DocTemplateGenerator {

    HashMap<String, String> bookmarks;
    XWPFDocument document;

    /**
     * 选择项，是否继承文档被替换部分的字体样式
     */
    public boolean applyFont = false;

    public DocTemplateGenerator(String filename) throws IOException {
        this.bookmarks = new HashMap<>();
        //解析docx模板并获取document对象
        TemplateFacade templateFacade = new TemplateFacade(filename);
        document = templateFacade.getDoc();
    }

    /**
     * 打开文件，返回一个生成器对象
     *
     * @param filename
     * @return
     */
    public static DocTemplateGenerator open(String filename) throws IOException {
        DocTemplateGenerator generator = new DocTemplateGenerator(filename);
        return generator;
    }

    public DocTemplateGenerator setApplyFont(Boolean val) {
        this.applyFont = val;
        return this;
    }

    /**
     * 保存到本地
     *
     * @param destName
     * @throws Exception
     */
    public void save(String destName) throws Exception {
//        DocTemplateGenerator.replaceByBookmark(document,bookmarks,false);
//        WordUtil.replaceWord(document,bookmarks);
        WordUtil2.replaceSS(document, bookmarks, false, this.applyFont);
        File dest = new File(destName);
        if (dest.exists()) {
            dest.delete();
            dest.createNewFile();
        }
        FileOutputStream fileOutputStream = new FileOutputStream(dest);
        document.write(fileOutputStream);
        fileOutputStream.flush();
        fileOutputStream.close();
    }

    public DocTemplateGenerator insertMap(int tableIndex, int row, List<String[]> data, InsertMapCellFunc cellFunc, InsertMapRowFunc rowFunc) {
        for (int i = 0; i < data.size(); i++) {
            String[] datas = data.get(i);
            XWPFTableRow row1 = document.getTables().get(tableIndex).insertNewTableRow(row + i);
            if(rowFunc!=null){
                rowFunc.rowDo(row1);
            }
            XWPFTableCell lastCell = null;
            for (int i1 = 0; i1 < datas.length; i1++) {
                String str = datas[i1];
                //  如果是被合并的单元格那么向前合并
                if (str.equals("@@合并")) {
                    XWPFTableCell cell = row1.createCell();
                    lastCell.getCTTc().addNewTcPr().addNewHMerge().setVal(STMerge.RESTART);
                    cell.getCTTc().addNewTcPr().addNewHMerge().setVal(STMerge.CONTINUE);
                } else {
                    XWPFTableCell cell = row1.createCell();
                    lastCell = cell;
                    cell.setText(str);
                    if (cellFunc != null) {
                        cellFunc.cellDo(cell);
                    }
                }
            }
        }
        return this;
    }

    @FunctionalInterface
    public interface InsertMapCellFunc {
        void cellDo(XWPFTableCell cell);
    }

    @FunctionalInterface
    public interface InsertMapRowFunc {
        void rowDo(XWPFTableRow row);
    }
}
